/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ttt.nor2.alarm;

import com.ttt.nor2.util.ExceptionUtil;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.log4j.Logger;

/**
 *
 * @author anuphabm
 */
public class Comm2File {

    private int count1;
    private InputStream io;
    private String path;
    //logger
    private Logger logger = Logger.getLogger(Comm2File.class);

    public Comm2File(String path) {
        count1 = 0;
        this.path = path;
    }

    public void setIo(InputStream io) {
        this.io = io;
    }

    private int chkDigitClassAlarm(int chin) {
        if (chin == 6) {
            return 1;
        } else {
            return 0;
        }

    }

    private String printArray(int data[]) {
        String bitValue = "";
        for (int i = 0; i < data.length; i++) {
            bitValue = bitValue + "[" + data[i] + "]";
        }
        return bitValue;
    }

    private String getBitClass(int data[]){
        String bitValue = "";
        for (int i = 0; i < data.length; i++) {
            bitValue = bitValue + data[i];
        }
        return bitValue;

    }

    public void process() {
        logger.debug("run function process!");
        int classAlarm[] = new int[6];
        int buffAlarm[] = new int[6];
        String out = "";
        try {
            int chIn = -1;
            ByteArrayOutputStream aos = new ByteArrayOutputStream();
            while ((chIn = io.read()) != -1) {
                // check value k = data || class
                //logger.debug("data[" + chIn + "]");
                if (chIn == 6 || chIn == 21) {
                    //logger.debug("count1[" + count1 + "]");
                    classAlarm[count1] = chkDigitClassAlarm(chIn);
                    logger.debug("char input[" + chIn + "]count[" + count1 + "]");
                    if (count1 == 5) {
                        count1 = 0;

                        out = new String(aos.toByteArray());                        
                        //write to file
                        ALarm2File alarm2file = new ALarm2File(getBitClass(buffAlarm), out, path);
                        alarm2file.createFile();       
                        logger.debug("output class " + printArray(buffAlarm));
                        logger.info("output data[" + out + "]");
                        logger.info("============================================================");

                        // reset buffer alarm
                        System.arraycopy(classAlarm, 0, buffAlarm, 0, 6);
                        aos.reset();
                        //count1++;
                    } else {
                        count1++;
                        continue;
                    }

                } else {// write to buffer
                    if(chIn == 10 ){// new line && spect no into buffer
                        aos.write(64);
                    }else if( chIn >=32 && chIn <= 125){
                        aos.write(chIn);
                    }
                    
                }

                //aos.write(k);
            }//end while

        } catch (Exception e) {
            logger.error("Exception read from comm port! " + ExceptionUtil.getStackTrace(e));
        }
    }
}
